package com.mpyy.handler.inventory;


import com.alibaba.fastjson.JSON;
import com.mpyy.pojo.InventoryChange;
import com.mpyy.pojo.InventoryDesc;
import com.mpyy.pojo.InventoryReceipts;
import com.mpyy.pojo.Paging;
import com.mpyy.service.inventory.InventoryDescServiceInter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * @version 1.0
 * @Description InventoryDescController
 * @Author JW
 * @Date 2022-02-23 16:38
 */
@RestController
public class InventoryDescController {
    @Autowired
    @Qualifier(value = "inventoryDescServiceImpl")
    private InventoryDescServiceInter inventoryDescServiceInter;

    /**
     * 获取库存商品明细数据
     * @param paging
     * @param inventoryDesc
     * @return
     */
    @RequestMapping(value = {"/searchInventoryDescProcess"})
    public String searchInventoryDescProcess(Paging paging, InventoryDesc inventoryDesc){
        //调用判断传入相关商品信息是否为商品编号方法
        judgeValueIsNumber(inventoryDesc);

        //通过set方法赋值
        inventoryDesc.setPaging(paging);

        //库存商品明细
        List<InventoryDesc> inventoryDescs = inventoryDescServiceInter.showAllInventoryDesc(inventoryDesc);

        //品种合计
        int inventoryDescCount = inventoryDescServiceInter.getInventoryDescCount(inventoryDesc);

        //创建一个Map
        HashMap<Object, Object> inventoryDescHashMap = new HashMap<>();
        inventoryDescHashMap.put("code",0);
        inventoryDescHashMap.put("msg","");
        inventoryDescHashMap.put("count",inventoryDescCount);
        inventoryDescHashMap.put("data",inventoryDescs);

        return JSON.toJSONString(inventoryDescHashMap);
    }

    /**
     * 获取库存商品汇总数据
     * @param inventoryDesc
     * @param paging
     * @return
     */
    @RequestMapping(value = {"/searchInventoryCollectInfo"},method = RequestMethod.GET)
    public String searchInventoryCollectInfo(InventoryDesc inventoryDesc, Paging paging){
        //调用判断传入相关商品信息是否为商品编号方法
        judgeValueIsNumber(inventoryDesc);
        System.out.println(inventoryDesc);

        //通过set方法赋值
        inventoryDesc.setPaging(paging);
        //库存商品汇总
        List<InventoryDesc> inventoryCollect = inventoryDescServiceInter.showAllInventoryCollect(inventoryDesc);

        //品种合计
        int inventoryCollectCount = inventoryDescServiceInter.getInventoryCollectCount(inventoryDesc).size();

        //创建一个Map
        HashMap<Object, Object> inventoryCollectMap = new HashMap<>();
        inventoryCollectMap.put("code",0);
        inventoryCollectMap.put("msg","");
        inventoryCollectMap.put("count",inventoryCollectCount);
        inventoryCollectMap.put("data",inventoryCollect);
        return JSON.toJSONString(inventoryCollectMap);
    }

    /**
     * 显示商品批次信息
     * @param proId
     * @param inventoryDesc
     * @return
     */
    @RequestMapping(value = {"/searchProBatchNumInfo/{proid}"},method = RequestMethod.GET)
    public String searchProBatchNumInfo(@PathVariable(name = "proid") String proId,InventoryDesc inventoryDesc){
        if("".equals(inventoryDesc.getInveDescDepot())){
            inventoryDesc.setInveDescDepot(null);
        }
        inventoryDesc.setProId(proId);
        List<InventoryDesc> proBatchNumInfo = inventoryDescServiceInter.getProBatchNumInfo(inventoryDesc);
        //创建一个Map
        HashMap<Object, Object> inventoryDescHashMap = new HashMap<>();
        inventoryDescHashMap.put("code",0);
        inventoryDescHashMap.put("msg","");
        inventoryDescHashMap.put("data",proBatchNumInfo);
        return JSON.toJSONString(inventoryDescHashMap);
    }

    /**
     * 根据仓库分组查询仓库库存明细
     * @param proId
     * @param inventoryDesc
     * @return
     */
    @RequestMapping(value = {"/searchInventoryDepotDesc/{proid}"},method = RequestMethod.GET)
    public String searchInventoryDepotDesc(@PathVariable(name = "proid") String proId, InventoryDesc inventoryDesc){
        inventoryDesc.setProId(proId);
        List<InventoryDesc> inventoryDescs = inventoryDescServiceInter.searchInventoryDepotDesc(inventoryDesc);
        //创建一个Map
        HashMap<Object, Object> inventoryDescHashMap = new HashMap<>();
        inventoryDescHashMap.put("code",0);
        inventoryDescHashMap.put("msg","");
        inventoryDescHashMap.put("data",inventoryDescs);
        return JSON.toJSONString(inventoryDescHashMap);
    }

    /**
     * 查询库存变动信息
     * @param inventoryDesc
     * @return
     */
    @RequestMapping(value = {"/searchInventoryChange"},method = RequestMethod.GET)
    public String searchInventoryChange(InventoryDesc inventoryDesc, Paging paging){
        //调用判断传入相关商品信息是否为商品编号方法
        judgeValueIsNumber(inventoryDesc);
        //通过set方法为Paging赋值
        inventoryDesc.setPaging(paging);
        //调用查询库存变动信息方法
        List<InventoryChange> inventoryChanges = inventoryDescServiceInter.searchInventoryChange(inventoryDesc);
        //调用获取库存变动信息条数方法
        int inventoryChangeCount = inventoryDescServiceInter.getInventoryChangeCount(inventoryDesc).size();
        //创建一个Map
        HashMap<Object, Object> inventoryChangesHashMap = new HashMap<>();
        inventoryChangesHashMap.put("code",0);
        inventoryChangesHashMap.put("msg","");
        inventoryChangesHashMap.put("count",inventoryChangeCount);
        inventoryChangesHashMap.put("data",inventoryChanges);
        return JSON.toJSONString(inventoryChangesHashMap);
    }

    /**
     * 查询商品月变动汇总信息
     * @param inventoryDesc 库存明细对象
     * @param paging 分页对象
     * @return
     */
    @RequestMapping(value = {"/searchWareMonthChange"},method = RequestMethod.GET)
    public String searchWareMonthChange(InventoryDesc inventoryDesc, Paging paging){
        //调用判断传入相关商品信息是否为商品编号方法
        judgeValueIsNumber(inventoryDesc);
        //通过set方法为Paging赋值
        inventoryDesc.setPaging(paging);
        //调用查询商品月变动汇总信息方法
        List<InventoryChange> inventoryChanges = inventoryDescServiceInter.searchWareMonthChange(inventoryDesc);

        //获取商品月变动汇总条数
        inventoryDesc.setPaging(null);
        int inventoryChangesCount = inventoryDescServiceInter.searchWareMonthChange(inventoryDesc).size();
        //创建一个Map
        HashMap<Object, Object> inventoryChangesHashMap = new HashMap<>();
        inventoryChangesHashMap.put("code",0);
        inventoryChangesHashMap.put("msg","");
        inventoryChangesHashMap.put("count",inventoryChangesCount);
        inventoryChangesHashMap.put("data",inventoryChanges);
        return JSON.toJSONString(inventoryChangesHashMap);
    }

    /**
     * 查询商品月变动明细信息
     * @param inventoryDesc 库存明细对象
     * @param paging 库存明细对象
     * @return
     */
    @RequestMapping(value = {"/searchWareMonthChangeDesc"},method = RequestMethod.GET)
    public String searchWareMonthChangeDesc(InventoryDesc inventoryDesc, Paging paging){
        //调用判断传入相关商品信息是否为商品编号方法
        judgeValueIsNumber(inventoryDesc);
        //通过set方法为Paging赋值
        inventoryDesc.setPaging(paging);
        //调用查询商品月变动明细信息方法
        List<InventoryChange> inventoryChanges = inventoryDescServiceInter.searchWareMonthChangeDesc(inventoryDesc);

        //获取商品月变动明细条数
        inventoryDesc.setPaging(null);
        int inventoryChangesCount = inventoryDescServiceInter.searchWareMonthChangeDesc(inventoryDesc).size();
        //创建一个Map
        HashMap<Object, Object> inventoryChangesHashMap = new HashMap<>();
        inventoryChangesHashMap.put("code",0);
        inventoryChangesHashMap.put("msg","");
        inventoryChangesHashMap.put("count",inventoryChangesCount);
        inventoryChangesHashMap.put("data",inventoryChanges);
        return JSON.toJSONString(inventoryChangesHashMap);
    }

    /**
     * 查询库存单据详情
     * @param inventoryDesc 库存明细对象
     * @param paging 库存明细对象
     * @return 库存单据详情
     */
    @RequestMapping(value = {"/searchInventoryReceiptsDesc"},method = RequestMethod.GET)
    public String searchInventoryReceiptsDesc(InventoryDesc inventoryDesc, Paging paging){
        //调用判断传入相关商品信息是否为商品编号方法
        judgeValueIsNumber(inventoryDesc);
        //通过set方法为Paging赋值
        inventoryDesc.setPaging(paging);
        //调用查询库存单据详情方法
        List<InventoryReceipts> inventoryReceipts = inventoryDescServiceInter.searchInventoryReceiptsDesc(inventoryDesc);
        //获取库存单据条数
        inventoryDesc.setPaging(null);
        int inventoryReceiptsCount = inventoryDescServiceInter.searchInventoryReceiptsDesc(inventoryDesc).size();
        //创建一个Map
        HashMap<Object, Object> inventoryReceiptsHashMap = new HashMap<>();
        inventoryReceiptsHashMap.put("code",0);
        inventoryReceiptsHashMap.put("msg","");
        inventoryReceiptsHashMap.put("count",inventoryReceiptsCount);
        inventoryReceiptsHashMap.put("data",inventoryReceipts);
        return JSON.toJSONString(inventoryReceiptsHashMap);
    }

    /**
     * 判断传入相关商品信息是否为商品编号
     * @param inventoryDesc 库存详情对象
     */
    public void judgeValueIsNumber(InventoryDesc inventoryDesc){
        //判断仓库编号和商品名称是否为空
        if("".equals(inventoryDesc.getInveDescDepot())){
            inventoryDesc.setInveDescDepot(null);
        }
        if("".equals(inventoryDesc.getInveDescName())){
            inventoryDesc.setInveDescName(null);
        }

        //判断相关商品信息是否为空
        if(null != inventoryDesc.getInveDescName()){
            //判断相关商品信息是否为商品编号
            boolean proNameIsNum = true;
            // 通过 length() 方法计算商品名称传入进来的字符串的长度，并将字符串长度存放到len中
            int len = inventoryDesc.getInveDescName().length();
            // 通过字符串长度循环
            for (int i = 0; i < len; i++) {
                // 判断每一个字符是否为数字，如果其中有一个字符不满足，则返回false
                if (!Character.isDigit(inventoryDesc.getInveDescName().charAt(i))) {
                    proNameIsNum = false;
                    break;
                }
            }
            //如果为商品编号
            if(proNameIsNum){
                inventoryDesc.setProId(inventoryDesc.getInveDescName());
                inventoryDesc.setInveDescName(null);
            }
        }
    }
}
